namespace PickGold.Charting.Design
{
    using System;
    using System.ComponentModel;
    using System.Drawing;
    using System.Drawing.Design;

    internal class MarkerStyleEditor : UITypeEditor, IDisposable
    {
        private ChartGraphics _chartGraph;
        private bool _disposed;

        public void Dispose()
        {
            this.Dispose(true);
            GC.SuppressFinalize(this);
        }

        protected virtual void Dispose(bool disposing)
        {
            if (!this._disposed)
            {
                if (disposing)
                {
                    this._chartGraph.Dispose();
                }
                this._disposed = true;
            }
        }

        ~MarkerStyleEditor()
        {
            this.Dispose(false);
        }

        public override bool GetPaintValueSupported(ITypeDescriptorContext context)
        {
            return true;
        }

        public override void PaintValue(PaintValueEventArgs e)
        {
            if (e.Value is MarkerStyle)
            {
                if (this._chartGraph == null)
                {
                    this._chartGraph = new ChartGraphics(null);
                }
                this._chartGraph.Graphics = e.Graphics;
                DataPointCustomProperties properties = null;
                if ((e.Context != null) && (e.Context.Instance != null))
                {
                    object instance = e.Context.Instance;
                    if (e.Context.Instance is Array)
                    {
                        Array array = (Array) e.Context.Instance;
                        if (array.Length > 0)
                        {
                            instance = array.GetValue(0);
                        }
                    }
                    if (instance is Series)
                    {
                        properties = (DataPointCustomProperties) instance;
                    }
                    else if (instance is DataPoint)
                    {
                        properties = (DataPointCustomProperties) instance;
                    }
                    else if (instance is DataPointCustomProperties)
                    {
                        properties = (DataPointCustomProperties) instance;
                    }
                    else if (instance is LegendItem)
                    {
                        properties = new DataPointCustomProperties();
                        properties.MarkerColor = ((LegendItem) instance).markerColor;
                        properties.MarkerBorderColor = ((LegendItem) instance).markerBorderColor;
                        properties.MarkerSize = ((LegendItem) instance).markerSize;
                    }
                }
                if ((properties != null) && (((MarkerStyle) e.Value) != MarkerStyle.None))
                {
                    PointF point = new PointF((e.Bounds.X + (((float) e.Bounds.Width) / 2f)) - 0.5f, (e.Bounds.Y + (((float) e.Bounds.Height) / 2f)) - 0.5f);
                    Color markerColor = (properties.MarkerColor == Color.Empty) ? Color.Black : properties.MarkerColor;
                    int markerSize = properties.MarkerSize;
                    if (markerSize > (e.Bounds.Height - 4))
                    {
                        markerSize = e.Bounds.Height - 4;
                    }
                    this._chartGraph.DrawMarkerAbs(point, (MarkerStyle) e.Value, markerSize, markerColor, properties.MarkerBorderColor, properties.MarkerBorderWidth, "", Color.Empty, 0, Color.Empty, RectangleF.Empty, true);
                }
            }
        }
    }
}

